<?php
// Start session management and include necessary functions
session_start();
require_once('../dao/database.php');
require_once('../dao/user_db.php');
require_once ('../dao/KilometerEntryDAO.php');
require_once ('../dao/SaisonDAO.php');
require_once ('../dao/user_db.php');
require_once ('../dto/SaisonSelection.php');
require_once('../util/Validation.php');
require_once ('../dao/SportartDAO.php');
require_once ('../dto/SportartSelection.php');

// Get the action to perform
if (isset($_POST['action'])) {
    $action = $_POST['action'];
} else if (isset($_GET['action'])) {
    $action = $_GET['action'];
} else {
    $action = 'show_result';
}

if (isset($_POST['submit'])) { 
    $action = 'show_result';    
}

// Perform the specified action
switch($action) {
    case 'show_result':

        $saisons = SaisonDAO::getSaisons();
        $saisonSelect = new SaisonSelection($saisons);

        $sportarten = SportartDAO::getAllSportarten();
        $sportartSelection = new SportartSelection($sportarten);
        
        /**
         * GET SAISON
         */
        if (isset($_GET['saison'])) {
            $saisonIndex = $_GET['saison'];
            if (is_numeric($saisonIndex)){
                $saisonSelect->setSelectedIndex(intval($saisonIndex));
            }
        }

        if (isset($_POST['saison'])) {
            $val = new Validation();
            $saison = $val->validateRequiredSaisonSelection("saison", "Diese Saison ist nicht gültig");
            $saisonSelect->setSelectedSaison($saison);
        }

        if (isset($_GET['tab'])) {
            $nextTab = $_GET['tab'];
            if (is_numeric($nextTab)){
                if (intval($nextTab) >= 1 && intval($nextTab) <= 4){
                    $sportartSelection->setSelectedIndex(intval($nextTab));
                    
                        //hole ergebnis aus db für tab
                } else {
                    $nextTab = 0;
                }
            } else {
                $nextTab = 0;
            }
        } else {
            $nextTab = 0;
        }


        //list beginn
        $count = KilometerEntryDAO::countAllVotesForSaison($saisonSelect->getSelectedSaisons(), $sportartSelection->getSelectedIndex());
        $gesamtPages = ceil(intval($count)/Constants::VAR_PAGE_SIZE);
        $newNextPage = 0;

        $from = 0;
        
        if (isset($_GET['page'])) {
            $nextPage = $_GET['page'];
            if (is_numeric($nextPage)){
                if (intval($nextPage) >= 0){ 
                        if ( intval($nextPage) < $gesamtPages ){
                            $from = $nextPage * Constants::VAR_PAGE_SIZE;
                            $newNextPage = $nextPage;
                        }
                }
            }
        }
        
        $totalSum = KilometerEntryDAO::getTotalSumFromSaison($saisonSelect->getSelectedSaisons(), $sportartSelection->getSelectedIndex());
        //null -> 0
        if ($totalSum == null) {
            $totalSum = 0;
        }
        
        
        $entries = get_best_users($from, Constants::VAR_PAGE_SIZE, $saisonSelect->getSelectedSaisons(), $sportartSelection->getSelectedIndex());

        $isFull = false;

        /**
        * GET resultSize
        */
        if (isset($_GET['size'])) {
            $sizeUnsave = $_GET['size'];
            if (is_numeric($sizeUnsave)){
                $size = intval($sizeUnsave);
                if ($size >= $count) {
                    $size = $count;
                    $isFull = true;
                }
            }
        } else {
            if ($count < 5) {
                $size = $count;
            } else {
                $size = 5;
            }
        }
        //list end
        
        include('resultView.php');
        break;
}
?>